package com.auxgroup.smarthome.inner.controller;

import com.auxgroup.bridge.app.inner.dto.AppUserDto;
import com.auxgroup.bridge.app.inner.vo.AppUserPushVo;
import com.auxgroup.bridge.app.inner.vo.AppUserVo;
import com.auxgroup.bridge.business.inner.vo.UserDeviceListVo;
import com.auxgroup.smarthome.basebean.Gender;
import com.auxgroup.smarthome.inner.service.AppUserDBService;
import com.auxgroup.smarthome.inner.service.DeviceInfoDBService;
import com.auxgroup.smarthome.syscode.Syscode;
import com.auxgroup.smarthome.utils.PageContent;
import com.auxgroup.smarthome.web.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * Created by kevinchen on 2017/7/24.
 */
@Api(description = "内部app服务", tags = "app用户相关")
@RestController
public class AppUserController {

    @Autowired
    private DeviceInfoDBService deviceInfoDBService;

    @Autowired
    private AppUserDBService appUserDBService;

    @ApiOperation(value = "创建第三方用户",notes = "调用paas平台openApi用户接口后调用此接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "openid", value = "openid", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "unionid", value = "unionid", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "nickName", value = "名字", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "headImg", value = "头像url", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "src", value = "第三方创建用户(wechat/qq)", required = true, paramType = "form", dataType = "string"),
    })
    @RequestMapping(value = "/otherUser", method = RequestMethod.POST)
    public ApiResponse<AppUserVo> createOtherUser(String uid,String openid,String unionid,String nickName,String headImg,String src){
        return appUserDBService.createOtherUser(uid,openid,unionid,nickName,headImg,src);
    }

    @ApiOperation(value = "创建用户",notes = "调用paas平台openApi用户接口后调用此接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "注册手机号码", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "uid", value = "用户id", required = true, paramType = "form", dataType = "string"),
    })
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public ApiResponse<AppUserVo> createAppUser(String uid, String phone){
        return appUserDBService.createPhoneUser(uid,phone);
    }

    @ApiOperation(value = "修改用户信息",notes = "调用paas平台openApi用户接口后调用此接口")
    /**@ApiImplicitParams({
            @ApiImplicitParam(name = "nickName", value = "昵称",  paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "realName", value = "姓名",  paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "headImg", value = "头像地址",  paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "gender", value = "性别,M（男）、F（女）、N（未知） ",  defaultValue = "M",paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "birthday", value = "出生日期 2017-07-25",  paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "country", value = "国家",defaultValue = "中国", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "region", value = "（ 中国）省、（美国）州、（泰国）府",  paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "city", value = "城市", paramType = "form", dataType = "string")
    })*/
    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    public ApiResponse<AppUserVo> updateAppUser(@RequestParam("uid") String uid,@RequestBody AppUserDto appUserDto){
        return appUserDBService.updateAppUser(uid,appUserDto);
    }

    @ApiOperation(value = "获取app用户列表信息",notes = "用户登录成功后记录用户的登录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keywords", value = "关键字：用户名/手机号/真实姓名", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "query", dataType = "int", defaultValue = "0"),
            @ApiImplicitParam(name = "size", value = "页尺寸", paramType = "query", dataType = "int", defaultValue = "20")
    })
    @GetMapping("/app/users")
    public ApiResponse<PageContent<AppUserVo>> getAppUserList(String keywords, Pageable pageable) {
        Page<AppUserVo> page = appUserDBService.getAppUserList(keywords, pageable);
        return ApiResponse.ok(new PageContent(page));
    }

    @ApiOperation(value = "获取用户信息",notes = "调用paas平台openApi用户接口后调用此接口")
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public ApiResponse<AppUserVo> getAppUser(@RequestParam("uid") String uid){
        return ApiResponse.ok(appUserDBService.findAppUser(uid));
    }

    @ApiOperation(value = "记录用户登录信息",notes = "用户登录成功后记录用户的登录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户uid", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "clientId", value = "终端设备唯一标识id", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "ip", value = "终端设备ip地址", paramType = "form", dataType = "string")
    })
    @RequestMapping(value = "/loginInfo", method = RequestMethod.POST)
    public ApiResponse recordLoginInfo(@ApiIgnore String uid, @ApiIgnore String clientId, @ApiIgnore String ip) {
        appUserDBService.recordLoginInfo(uid,clientId,ip);
        return ApiResponse.prompt(Syscode.SC_OK);
    }

    @ApiOperation(value="筛选用户信息",notes = "使用推送消息的时候需要调用用户信息")
    @RequestMapping(value = "/filterUsers",method = RequestMethod.GET)
    public ApiResponse<List<AppUserPushVo>> filterUsers(@RequestParam(value="gender") String gender,
                                                        @RequestBody Map<String,String> map){
        if(!Gender.genderNames().contains(gender) ||
                map==null || map.size() > 5){
            return ApiResponse.prompt(Syscode.DTO_PARAMS_ERROR);
        }
        return appUserDBService.filterUsers(gender,map);
    }

    @ApiOperation(value = "获取app用户设备列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "int", defaultValue = "0"),
            @ApiImplicitParam(name = "size", value = "页尺寸", paramType = "query", dataType = "int", defaultValue = "20")
    })
    @GetMapping(value = "/app/users/{uid}/devices")
    public ApiResponse<PageContent<UserDeviceListVo>> getUserDeviceList(@PathVariable("uid") String uid, @ApiIgnore Pageable pageable) {
        Page<UserDeviceListVo> page = deviceInfoDBService.getUserDeviceList(uid, pageable);
        return ApiResponse.ok(new PageContent(page));
    }

    @ApiOperation(value = "获取所有app用户")
    @GetMapping(value = "/app/users/all")
    public ApiResponse<List<AppUserVo>> getAllUserList(@RequestParam(value = "keywords", required = false) String keywords) {
        List<AppUserVo> appUserVos = appUserDBService.getAllAppUser(keywords);
        return ApiResponse.ok(appUserVos);
    }

    @ApiOperation(value = "绑定第三方帐号",notes = "绑定第三方帐号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "openid", value = "openid", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "qqid", value = "qqid", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "form", dataType = "string"),
    })
    @RequestMapping(value = "/bindOtherAccount", method = RequestMethod.PUT)
    public ApiResponse bindOtherAccount(String uid,String openid,String qqid,String phone){
        return appUserDBService.bindOtherAccount(uid,openid,qqid,phone);
    }

    @ApiOperation(value = "查找第三方关联帐号",notes = "查找第三方关联帐号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openid", value = "openid", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "qqid", value = "qqid", required = true, paramType = "form", dataType = "string"),
    })
    @RequestMapping(value = "/findOtherUser", method = RequestMethod.GET)
    public ApiResponse<List<AppUserVo>> findOtherUser(String openid,String qqid){
        return appUserDBService.findByOpenid(openid,qqid);
    }

    @ApiOperation(value = "通过手机号获取用户信息",notes = "通过手机号获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "form", dataType = "string")
    })
    @RequestMapping(value = "/userByPhone", method = RequestMethod.GET)
    public ApiResponse<AppUserVo> getAppUserByPhone(String phone){
        return ApiResponse.ok(appUserDBService.findAppUserByPhone(phone));
    }
}
